package com.zoo.boot.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.zoo.boot.bean.UserEntity;
import com.zoo.boot.dao.UserDao;
import com.zoo.boot.mapper.LRoleUserMapper;
import com.zoo.boot.mapper.LUserMapper;
import com.zoo.boot.model.LRoleUser;
import com.zoo.boot.model.LRoleUserExample;
import com.zoo.boot.model.LUser;
import com.zoo.boot.model.LUserExample;
import com.zoo.boot.service.UserService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 用户信息管理 Service
 *
 * @Author: WangB
 * @Date: 2021/6/25
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private LUserMapper mapper;

    @Resource
    private LRoleUserMapper lRoleUserMapper;

    @Resource
    private UserDao userDao;


    /**
     * 根据登录名获取用户信息
     *
     * @param username
     * @return
     */
    @Override
    public LUser loginByUsername(String username) {
        LUserExample lUserExample = new LUserExample();
        lUserExample.createCriteria().andUserNameEqualTo(username);
        List<LUser> lUsers = mapper.selectByExample(lUserExample);
        return lUsers.get(0);
    }

    /**
     * 模糊查询
     *
     * @param user
     * @return
     */
    @Override
    public List<LUser> selectUsers(LUser user) {
        LUserExample lUserExample = new LUserExample();
        if (StrUtil.isNotEmpty(user.getUserName())) {
            lUserExample.createCriteria().andUserNameLike("%" + user.getUserName() + "%");
        }
        List<LUser> lUsers = mapper.selectByExample(lUserExample);
        return lUsers;
    }


    /**
     * 根据userId获取用户信息
     *
     * @param userId
     * @return
     */
    @Override
    public UserEntity selectUserById(Integer userId) {
        return userDao.selectUserById(userId);
    }

    /**
     * 添加用户信息
     *
     * @param userEntity
     * @return
     */
    @Transactional
    @Override
    public boolean insertUser(UserEntity userEntity) {
        LUser lUser = new LUser();
        BeanUtil.copyProperties(userEntity, lUser);
        lUser.setStatus(false);
        lUser.setCreateTime(new Date());
        lUser.setUpdateTime(new Date());
        lUser.setPassword(new BCryptPasswordEncoder().encode(lUser.getPassword()));
        int var1 = mapper.insert(lUser);

        LRoleUser lRoleUser = new LRoleUser();
        BeanUtil.copyProperties(userEntity, lRoleUser);
        lRoleUser.setUserId(lUser.getUserId());
        int var2 = lRoleUserMapper.insert(lRoleUser);
        return (var1 + var2) > 1;
    }


    /**
     * 修改 用户信息
     *
     * @param userEntity
     * @return
     */
    @Transactional
    @Override
    public boolean updateUser(UserEntity userEntity) {
        //修改用户信息
        LUser lUser = new LUser();
        BeanUtil.copyProperties(userEntity, lUser);
        lUser.setUpdateTime(new Date());
        int i = mapper.updateByPrimaryKeySelective(lUser);

        LRoleUser lRoleUser = new LRoleUser();
        BeanUtil.copyProperties(userEntity, lRoleUser);
        lRoleUser.setUserId(lUser.getUserId());

        //先删除用户角色关联表记录，再添加记录
        LRoleUserExample lRoleUserExample = new LRoleUserExample();
        lRoleUserExample.createCriteria().andUserIdEqualTo(lUser.getUserId());
        int i1 = lRoleUserMapper.deleteByExample(lRoleUserExample);
        int var2 = lRoleUserMapper.insert(lRoleUser);

        return (i + i1 + var2) > 1;
    }


    /**
     * 根据用户id删除用户信息
     *
     * @param userId
     * @return
     */
    @Transactional
    @Override
    public boolean deleteUserById(Integer userId) {
        int i = mapper.deleteByPrimaryKey(userId);
        LRoleUserExample lRoleUserExample = new LRoleUserExample();
        lRoleUserExample.createCriteria().andUserIdEqualTo(userId);
        int i1 = lRoleUserMapper.deleteByExample(lRoleUserExample);

        return i + i1 > 1;
    }


}
